// pages/upsalary/upsalary.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var deptno = options.deptno;
    var dname = options.dname;
    var that = this;
    this.setData({
      deptno : deptno,
      dname: dname
    })
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//getEnameGroupDept',
      data:{
        deptno: deptno
      },method:'GET',
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res)
        var ename = [];var empno = [];var orsal = [];
        for (var i = 0; i < res.data.length ; i++){
          console.log(res.data[i].ename)
          ename.push(res.data[i].ename)
          empno.push(res.data[i].empno)
          orsal.push(res.data[i].sal)
        }
        console.log(ename)
        that.setData({
          ename: ename,
          empno: empno,
          orsal: orsal,
          index: 0
        })
      }
    })
  },
  EnameChange:function(e){
    this.setData({
      index: e.detail.value
    })
  },
  forsubmit:function(e){
    var ename = this.data.ename[this.data.index];
    var empno = e.detail.value.empno;
    var dname = e.detail.value.dname;
    var deptno = this.data.deptno;
    var orsal = e.detail.value.orsal;
    var newsal = e.detail.value.newsal;
    var reason = e.detail.value.reason;
    if (newsal == ""){
      wx.showToast({
        title: '申请薪金不能为空',
        icon:'none'
      })
    } else if (reason == ""){
      wx.showToast({
        title: '申请原因不能为空',
        icon: 'none'
      })
    }else{
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//insertSalaryInfo',
        data: {
          ename: ename,
          empno: empno,
          deptno: deptno,
          orsal: orsal,
          newsal: newsal,
          reason: reason
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          var msg = res.data;
          if (msg === '申请成功') {
            wx.showToast({
              title: msg,
              icon: 'success'
            })
            setTimeout(function () {
              wx.navigateBack({
              })
            }, 1500)
          } else {
            wx.showToast({
              title: msg,
              icon: 'none'
            })
          }
        }
      })
    }
   

  }
})